﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DEWY_SSIS_CONFIG_MANAGER
{
    public class EditableItems
    {
        public String ConfigurableName { get; set; }
        public String Path { get; set; }
        public String Type { get; set; }
        public String Value { get; set; }
        public String Description { get; set; }
        


        public EditableItems()
        {

        }

        public EditableItems(String ConfigName, String Path, String Type, String Value, String Description)
        {
            this.ConfigurableName = ConfigName;
            this.Path = Path;
            this.Value = Value;
            this.Type = Type;
            this.Description = Description;
        }


        public Panel UI()
        {
            try
            {
                const int CONTROL_TOP = 10;
                Panel pnl = new Panel()
                {
                    Name = String.Format("sub_pnl_{0}", this.Path),
                    Dock = System.Windows.Forms.DockStyle.Top,
                    Location = new System.Drawing.Point(0, 0),
                    Tag = this,
                    Size = new System.Drawing.Size(700, 32),
                };

            Label lbl_Cap =
                new Label
            {
                Name = "lbl" + this.ConfigurableName,
                Tag = String.Format("_LABEL_{0}",this.Path),
                Text = this.ConfigurableName,
                AutoSize = true,
                Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top   | System.Windows.Forms.AnchorStyles.Left))),
                Location = new System.Drawing.Point(10, CONTROL_TOP + 3)
        
            };

                

            pnl.Controls.Add(lbl_Cap);

            int editRightX;
      
            
            switch (this.Type.ToLower())
            {
                case "datetime":
                    DateTime timeMan = new DateTime();
                    String DateOnly = this.Value.Remove(this.Value.IndexOf(' '));
                    String TimeOnly = this.Value.Substring(this.Value.IndexOf(' '),this.Value.Length-DateOnly.Length).Trim();
                    timeMan = DateTime.ParseExact(DateOnly, new string[] { "M/d/yyyy", "d/M/yyyy" }, null, System.Globalization.DateTimeStyles.AssumeUniversal);

                    pnl.Controls.Add(new DateTimePicker
                    {

                        Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)| System.Windows.Forms.AnchorStyles.Right))),
                        Name = "dtp_" + this.ConfigurableName,
                        Tag = String.Format("_DATE_VALUE_{0}", this.Path),
                        Text = timeMan.ToShortDateString(),
                        Size = new System.Drawing.Size(150, 30),
                        TabStop = true,
                    	Location = new System.Drawing.Point((lbl_Cap.Width > 140) ? 150 + lbl_Cap.Width / 8 : 150, CONTROL_TOP),
                    });
                    TextBox HourMinutSeconds = new TextBox
                    {
                        Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))),
                        Name = "txtBx_" + this.ConfigurableName,
                        Tag = String.Format("_TIME_VALUE_{0}", this.Path),
                        Text = TimeOnly,
                        Size = new System.Drawing.Size(150, 30),
                        Location = new System.Drawing.Point(350, CONTROL_TOP),
                        TabStop = true
                    };
                    HourMinutSeconds.Enter += new EventHandler(HourMinutSeconds_GotFocus);
                    HourMinutSeconds.Leave += new EventHandler(HourMinutSeconds_Leave);
                    pnl.Controls.Add(HourMinutSeconds);
                    editRightX = HourMinutSeconds.Right;
                    break;
                case "boolean":
                    CheckBox cb = new CheckBox();
                    cb.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
                    cb.Name = "cb_" + this.ConfigurableName;
                    cb.Tag = String.Format("_BOOL_VALUE_{0}", this.Path);
                    //cb.Text = this.Value;
                    cb.Size = new System.Drawing.Size(150, 30);
                    cb.TabStop = true;
                        cb.Location = new System.Drawing.Point(350, CONTROL_TOP - 4);
                    cb.Checked = !this.Value.Equals("0");
                    pnl.Controls.Add(cb);
                        editRightX = cb.Right;
                    break;
                case "empty":
                    TextBox txt_empty = new TextBox
                    {

                        Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))),
                        Name = "txt_" + this.ConfigurableName,
                        Tag = String.Format("_DBNULL_VALUE_{0}", this.Path),
                        Text = this.Type + " (DbNull) ",
                        Enabled = false,
                        Size = new System.Drawing.Size(250, 20),
                            Location = new System.Drawing.Point((lbl_Cap.Width > 140) ? 150 + lbl_Cap.Width / 2 : 150, CONTROL_TOP - 4),
                        //Multiline = true,
                        //ScrollBars = ScrollBars.Vertical


                    };
                    pnl.Controls.Add(txt_empty);
                        editRightX = txt_empty.Right;
                    break;
                default:
                    TextBox txt_data_default = new TextBox
                    {

                        Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))),
                        Name = "txt_" + this.ConfigurableName,
                        Tag = String.Format("TEXT_VALUE_{0}", this.Path),
                        Text = this.Value,
                        Size = new System.Drawing.Size(250, 30),
                            Location = new System.Drawing.Point((lbl_Cap.Width > 140) ? 150 + lbl_Cap.Width / 2 : 150, CONTROL_TOP),
                        TabStop = true
                        //Margin=new Padding(lbl_Cap.Width+100,0,lbl_Cap.Width/2,0)
                        //Multiline = true,
                        //ScrollBars = ScrollBars.Vertical

                    };

                    txt_data_default.Leave += new EventHandler(txt_data_default_Leave);
                    txt_data_default.Enter += new EventHandler(txt_data_default_Enter);
                    pnl.Controls.Add(txt_data_default);
                        editRightX = txt_data_default.Right;

                    break;
            }
                Label descrLabel = new Label
                {
                    Anchor = AnchorStyles.Top | AnchorStyles.Right,
                    Name = "lblDescr_" + this.ConfigurableName,
                    Text = this.Description,
                    AutoSize = false,
                    Size = new System.Drawing.Size(pnl.Width - editRightX - 10 - 10, 27),
                    Location = new System.Drawing.Point(editRightX + 10, CONTROL_TOP - 4),
                    TextAlign = System.Drawing.ContentAlignment.MiddleLeft
                };
                pnl.Controls.Add(descrLabel);
            return pnl;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }


        void txt_data_default_Enter(object sender, EventArgs e)
        {
            try
            {
                (sender as TextBox).BorderStyle = BorderStyle.FixedSingle;
                (sender as TextBox).BackColor = System.Drawing.Color.LightGreen;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        void HourMinutSeconds_Leave(object sender, EventArgs e)
        {
            try
            {
                (sender as TextBox).BorderStyle = BorderStyle.Fixed3D;
                (sender as TextBox).BackColor = System.Drawing.Color.White;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        void HourMinutSeconds_GotFocus(object sender, EventArgs e)
        {
            try
            {
                (sender as TextBox).BorderStyle = BorderStyle.FixedSingle;
                (sender as TextBox).BackColor = System.Drawing.Color.LightGreen;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }

        }

        void txt_data_default_Leave(object sender, EventArgs e)
        {
            try
            {
                switch (this.Type.ToLower())
                {
                    case "int32":
                        int dumbint32;
                        if (!Int32.TryParse((sender as TextBox).Text, out dumbint32))
                        {
                            MessageBox.Show("Invalid Integer32 Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "uint32":
                        uint dumbuint32;
                        if (!UInt32.TryParse((sender as TextBox).Text, out dumbuint32))
                        {
                            MessageBox.Show("Invalid UInt32 Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "int64":
                        long dumbint64;
                        if (!Int64.TryParse((sender as TextBox).Text, out dumbint64))
                        {
                            MessageBox.Show("Invalid Integer64 Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "uint64":
                        UInt64 dumbunit64;
                        if (!UInt64.TryParse((sender as TextBox).Text, out dumbunit64))
                        {
                            MessageBox.Show("Invalid UInt64 Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "double":
                        Double dumbdouble;
                        if (!Double.TryParse((sender as TextBox).Text, out dumbdouble))
                        {
                            MessageBox.Show("Invalid Double Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "byte":
                        Byte dumbbyte;
                        if (!Byte.TryParse((sender as TextBox).Text, out dumbbyte))
                        {
                            MessageBox.Show("Invalid Byte Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "char":
                        Char dumbchar;
                        if (!Char.TryParse((sender as TextBox).Text, out dumbchar))
                        {
                            MessageBox.Show("Invalid Char Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "int16":
                        Int16 dumbint16;
                        if (!Int16.TryParse((sender as TextBox).Text, out dumbint16))
                        {
                            MessageBox.Show("Invalid Int16 Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "uint16":
                        UInt16 dumbuint16;
                        if (!UInt16.TryParse((sender as TextBox).Text, out dumbuint16))
                        {
                            MessageBox.Show("Invalid UInt16 Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "sbyte":
                        SByte dumbsbyte;
                        if (!SByte.TryParse((sender as TextBox).Text, out dumbsbyte))
                        {
                            MessageBox.Show("Invalid SByte Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    case "single":
                        Single dumbsingle;
                        if (!Single.TryParse((sender as TextBox).Text, out dumbsingle))
                        {
                            MessageBox.Show("Invalid Single Value");
                            (sender as TextBox).Text = this.Value;
                        }
                        break;
                    default:
                        break;

                }
                (sender as TextBox).BorderStyle = BorderStyle.Fixed3D;
                (sender as TextBox).BackColor = System.Drawing.Color.White;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }


    }
}
